﻿using CMS.Auth;
using CMS.Model;
using CMS.ServiceProvider;
using CMS.Utils;
using Microsoft.AspNetCore.Mvc;

namespace CMS.Controllers
{
    public class UserController:BaseController<UserModel,UserServiceProvider>
    {
        [HttpPost]
        public virtual ResponseData<String> GetToken(dynamic data)
        {
            var user = data.user?.ToString();
            var password = data.password?.ToString();
            var token = serviceProvider.TryLogin(user, password);
            var optUser = ServiceLoader.GetOptUser();
            new UserTokenManager().SetupUserFromToken(optUser, token);
            var result = new ResponseData<String>(token);
            return result;
        }

        [HttpPost,MyAuthorize]
        public virtual ResponseData<String> ChangePassword(dynamic data)
        {
            var user = data.user?.ToString();
            var password = data.password?.ToString();
            var validPassword = data.validPassword?.ToString();
            serviceProvider.ChangePassword(user, password, validPassword);
            var result = new ResponseData<String>("操作成功");
            return result;
        }
    }
}
